// pages/travel/detail.js
var WxParse = require('../../wxParse/wxParse.js');
import {
  platform,
  user,
  hotel,
  wxapp,
  tour
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    status: false,
    distance_new: '',
    infos: '',
    has_collect: 0,
    latitude: '',
    longitude: '',
    loadMoreText: '加载中...', //底部文字
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id
    this.setData({
      id
    })

  },
  // 获取列表
  getList(page) {
    console.log(this.distanceMy)
    let that = this
    hotel.lists({
      page: page,
      size: 10,
      orderway: 'asc',
      orderby: 'distance',
      longitude: this.data.longitude,
      latitude: this.data.latitude
    }, res => {
      that.setData({
        total: res.data.total
      })
      if (res.data.list.length > 0) {
        wx.getLocation({
          type: 'wgs84',
          success(re) {
            const latitude = re.latitude
            const longitude = re.longitude
            res.data.list.forEach(item => {
              item.distance_new = that.distance(latitude, longitude, item.latitude, item.longitude)
            })
            that.setData({
              latitude,
              longitude,
              list: that.data.list.concat(res.data.list) //合并数据
            })
            if (11 > res.data.list.length > 0) {
              that.setData({
                loadMoreText: '' //没有更多了
              })
            }
          },
          fail(re) {
            that.setData({
              list: that.data.list.concat(res.data.list) //合并数据
            })
            if (11 > res.data.list.length > 0) {
              that.setData({
                loadMoreText: '' //没有更多了
              })
            }
            // wx.showToast({
            //     title: '定位失败',
            //     icon: 'error'
            // })
          }
        })


      } else {
        if (page !== 1) {
          that.setData({
            loadMoreText: '暂无数据'
          })
        } else {
          that.setData({
            list: [],
            loadMoreText: '暂无数据'
          })
        }
      }
      wx.stopPullDownRefresh();
    })
  },
  distance(la1, lo1, la2, lo2) {
    var La1 = (la1 * Math.PI) / 180.0
    var La2 = (la2 * Math.PI) / 180.0
    var La3 = La1 - La2
    var Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
    var s =
      2 *
      Math.asin(
        Math.sqrt(
          Math.pow(Math.sin(La3 / 2), 2) +
          Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
        )
      )
    s = s * 6378.137
    s = Math.round(s * 10000) / 10000
    s = s.toFixed(2)
    return s
  },
  toCall() {
    let phone = this.data.infos.telephone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  toUrl(e) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  // 收藏
  toCollect() {
    user.collect.submit({
      module: 'scenic_spot',
      module_id: this.data.infos.id
    }, res => {
      if (res.code == 1) {
        wx.showToast({
          title: res.data.message,
        })
        let has_collect = this.data.has_collect == 0 ? 1 : 0
        this.setData({
          has_collect
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  toMap() {
    let infos = this.data.infos
    let latitude = Number(infos.latitude)
    let longitude = Number(infos.longitude)
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.openLocation({ //​使用微信内置地图查看位置。
          latitude: latitude, //要去的纬度-地址
          longitude: longitude, //要去的经度-地址
          name: infos.name,
          address: infos.address
        })
      }
    })
  },
  distance(la1, lo1, la2, lo2) {
    var La1 = (la1 * Math.PI) / 180.0
    var La2 = (la2 * Math.PI) / 180.0
    var La3 = La1 - La2
    var Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
    var s =
      2 *
      Math.asin(
        Math.sqrt(
          Math.pow(Math.sin(La3 / 2), 2) +
          Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
        )
      )
    s = s * 6378.137
    s = Math.round(s * 10000) / 10000
    s = s.toFixed(2)
    return s
  },
  toPre(e) {
    let i = e.currentTarget.dataset.img
    let imgs = this.data.infos.images
    wx.previewImage({
      urls: imgs,
      current: i
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this
    that.getInfo()
    tour.product.detail({
      product_id: that.data.id
    }, res => {
      if (res.code == 1) {
        this.setData({
          infos: res.data
        })
        WxParse.wxParse('bindName', 'html', res.data.content, this, 5)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  getDetail() {
    tour.product.detail({
      product_id: this.data.id
    }, res => {
      if (res.code == 1) {
        this.setData({
          infos: res.data
        })
        WxParse.wxParse('bindName', 'html', res.data.content, this, 5)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  getInfo() {
    let token = wx.getStorageSync('token')
    if (token) {
      // 获取用户详情
      user.detailS({}, res => {
        if (res.code == 20003) {
          this.getUserInfos()
        } else {

        }

      })
    } else {
      this.getUserInfos()

    }
  },
  /**
   * 授权登录
   */
  getUserInfos(e) {
    let that = this
    wx.login({
      success(res) {
        console.log(res)
        let code = res.code
        wx.getUserInfo({
          success: (res) => {
            var encryptedData = res.encryptedData;
            var iv = res.iv
            var info = res.rawData
            var obj = {
              code: code,
              iv: iv,
              encryptedData: encryptedData
            }
            wxapp.login(obj, res => {
              if (res.code == 1) {
                wx.setStorage({
                  key: "token",
                  data: res.data.token
                })
                that.getDetail()
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            })
          },
          fail: res => {

          }
        })
      }
    })
  },
  toOrder() {
    let that = this
    let code = wx.getStorageSync('code')
    wx.showModal({
      title: '提示',
      content: '游玩路线三选一，确定要领取该路线卡券吗？',
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          tour.order.submit({
            product_id: this.data.id,
            redeem_code: code
          }, res => {
            if (res.code == 1) {
              wx.showToast({
                title:'领取成功',
              })
              setTimeout(() => {
                wx.redirectTo({
                  url: '/pages/couponRoute/index',
                })
              }, 1000)
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'error'
              })
            }
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // pageNum1 = 1
    // this.setData({
    //   list: [],
    // });
    // this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // pageNum1 += 1;
    // this.getList(pageNum1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})